#ifndef COMMON_THISTHREAD_H_
#define COMMON_THISTHREAD_H_
#include <pthread.h>
#include <fermat/common/timestamp.h>
#include <fermat/common/timespan.h>
#include <string>

namespace fermat {
namespace this_thread {

extern __thread pthread_t t_tid;

void cacheTid();

inline pthread_t tid()
{
    if (__builtin_expect(t_tid == 0, 0))
    {
      cacheTid();
    }
    return t_tid;
}

    void setThreadName(pthread_t thread, const std::string &threadName);

    /*!
    * @return true if the thread is main thread pid = thread_id
    */
    bool isMainThread();

    /*!
    * Suspends the current thread for the specified
    * amount of time.
    */
    bool sleep_for(Timespan span);

    /*!
    * Suspends the current thread for the specified
    * amount of time, a another way to sleep for sleep.
    */
    bool sleep_until(Timestamp stamp);

	  /*!
	  * Yields cpu to other threads.
	  */
    void yield();
	  /*!
	  * exit current thread.
	  */
    void exit();

}

}
#endif
